package com.atomic.test.example.controller;

import com.atomic.test.example.common.ErrorCode;
import com.atomic.test.example.common.Result;
import com.atomic.test.example.entity.Post;
import com.atomic.test.example.query.CreatePostDo;
import com.atomic.test.example.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.atomic.test.example.utils.CheckDataUtil.insertPostCheck;


/**
 * Created by dreamyao on 2017/3/9.
 */
@Controller
@RequestMapping("/post")
public class PostController {

    @Autowired
    private IPostService postService;

    /**
     * 创建或回复帖子
     * @param createPostDo
     * @return
     */
    @ResponseBody
    @RequestMapping("/insertPost")
    public Result<Integer> insertPost(@RequestBody CreatePostDo createPostDo) {
        if (!insertPostCheck(createPostDo)) {
            return new Result<>(ErrorCode.PARAMETER_ERROR, "参数错误！");
        }
        Integer num = postService.insertPost(createPostDo);
        return new Result<>(num);
    }

    /**
     * 根据帖子标题查看帖子及所有回复
     * @param postTitle
     * @return
     */
    @ResponseBody
    @RequestMapping("/getPostByPostTitle")
    public Result<List<Post>> getPostByPostTitle(String postTitle){
        if (postTitle.equals("") || postTitle.toCharArray().length > 50) {
            return new Result<>(ErrorCode.PARAMETER_ERROR, "参数错误！");
        }
        List<Post> posts = postService.getPostByPostTitle(postTitle);
        return new Result<>(posts);
    }
}
